package com.dmx.fw.util;

/**
 * 数据导出到Excel文件的基本实现类
 * @author stxidt(meitao)
 */

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class DataToExcel {
	
    private List fieldList;//表字段名(String)

    private List fieldValueList;//表内容(每行一个Map<field,value>)

    private Map fieldMap;//(表字段名,中文描述)

    private String sheetName="Noname";//工作表名

    private int sheetSort;//工作表序号

    private String addCellStr;//以分号(;)隔开各列值

    private int colSize=0;

    HSSFWorkbook workbook=new HSSFWorkbook();//产生工作簿������

    public DataToExcel() {
    }

    public void setFieldList(List fieldList) {
        this.fieldList = fieldList;
        if(this.fieldList!=null){
            this.colSize = this.fieldList.size();
        }
    }

    public void setFieldValueList(List fieldValueList) {
        this.fieldValueList = fieldValueList;
    }

    public void setFieldMap(Map fieldMap) {
        this.fieldMap = fieldMap;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    public void setSheetSort(int sheetSort) {
        this.sheetSort = sheetSort;
    }

    public List getFieldList() {
        return fieldList;
    }

    public List getFieldValueList() {
        return fieldValueList;
    }

    public Map getFieldMap() {
        return fieldMap;
    }

    public String getSheetName() {
        return sheetName;
    }

    public int getSheetSort() {
        return sheetSort;
    }

    public void addSheet(){
        if(this.fieldList==null||this.fieldList.size()<=0)return;

        HSSFSheet sheet=this.workbook.createSheet();//产生工作表
        //this.setSheet();

        HSSFRow row=sheet.createRow(0);//表字段行
        this.addCell(this.fieldMap,row);
        if(this.addCellStr!=null){
            this.addCell(row,(short)this.colSize);
        }

        Map valueMap=null;
        for(int i=0;i<this.fieldValueList.size();i++){
            row=sheet.createRow(i+1);
            valueMap=(Map)this.fieldValueList.get(i);
            this.addCell(valueMap,row);
        }
    }

    public void clearForNewSheet(){
        this.fieldList=null;
        this.fieldMap=null;
        this.fieldValueList=null;
        this.sheetName="Noname";
        this.sheetSort++;
    }

    public void outPut(OutputStream out)throws IOException{
        this.workbook.write(out);
    }

    public byte[] getBytes(){
        return this.workbook.getBytes();
    }

    public void addTableTitileCell(String description){
        this.addCellStr=description;
    }

    private void addCell(HSSFRow row,short startIndex){
        if(this.addCellStr!=null){
            String[] arr=this.addCellStr.split(";");
            for(int i=0;i<arr.length;i++){
                HSSFCell cell=row.createCell((short)(i+startIndex));
                cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                cell.setCellValue(arr[i]);
            }
        }
    }

    private void setSheet(){
        this.workbook.setSheetName(this.sheetSort,this.sheetName,HSSFWorkbook.ENCODING_UTF_16);
    }

    private void addCell(Map valueMap,HSSFRow row){
        for(int i=0;i<this.fieldList.size();i++){
            HSSFCell cell=row.createCell((short)i);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(nullTreate(valueMap.get(this.fieldList.get(i))));
        }
    }

    private String nullTreate(Object obj){
        if(obj==null)return "";
        return obj.toString();
    }

}
